package com.jake.springboot.mybatis.service.impl;

import com.jake.springboot.mybatis.domain.Customer;
import com.jake.springboot.mybatis.mapper.CustomerMapper;
import com.jake.springboot.mybatis.mapper.IdentityCardMapper;
import com.jake.springboot.mybatis.mapper.MobilePhoneMapper;
import com.jake.springboot.mybatis.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private IdentityCardMapper cardMapper;

    @Autowired
    private MobilePhoneMapper phoneMapper;

    @Override
    public Customer getById(Integer customerId) {
        return customerMapper.getById(customerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(Customer customer, int customerId) {
        int effect = customerMapper.updateById(customer, customerId);
        phoneMapper.updateByCustomerId(customer.getPhones(), customerId);
        cardMapper.updateByCustomerId(customer.getCard(), customer.getCard().getCardId());
        return effect;
    }
}
